//
//  HZHomeCell.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/20.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit
import SnapKit



let HZNameTextFont:CGFloat = 14
let HZTimeTextFont:CGFloat = 10
let HZSpacing:CGFloat = 10
let HZIconSize = 35
let HZItemSize = 35
let HZItemMagin = 5.0



class HZHomeCell: UITableViewCell {

    
    //定义一个约束用来更新,需要导入第三方约束的框架才可以用这个属性
    var constraintTop: Constraint?
    
  
    //定义一个模型的 VM
    var cellVM:HZHomeCellVm?{
        
        //一拿到数据就赋值
        didSet{
            
            //先取消这个约束
            constraintTop?.uninstall()
            
            ownView.cellVM = cellVM
            
            //如果没有转发微博就隐藏显示转发的内容
            guard let _ = cellVM?.cellModel?.retweeted_status else {
                
                otherView.hidden = true
                
                //更新约束
                statusView.snp_updateConstraints(closure: { (make) -> Void in
                     constraintTop = make.top.equalTo(ownView.snp_bottom).constraint
                })
                
                 return
            }
            
            //有转发微博就显示
             otherView.hidden = false
            otherView.cellVM = cellVM
            
            //更新约束
            statusView.snp_updateConstraints(closure: { (make) -> Void in
                constraintTop = make.top.equalTo(otherView.snp_bottom).constraint
            })


            
        }

        
    }
    

    // MARK: -  代码创建初始化的方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        //让 cell没有点击效果
        selectionStyle = .None
        
        
        
        //添加子控件
        setupUI()
        
        
        //设置 cell 的背景色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // MARK: -  添加子控件
    private func setupUI(){
        
        contentView.addSubview(ownView)
        contentView.addSubview(statusView)
        contentView.addSubview(otherView)

        //约束
        ownView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(0)
            
            make.top.equalTo(10)
            //            make.height.equalTo(80)
        }
        
        
        //转载
        otherView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(ownView.snp_bottom)
            make.leading.trailing.equalTo(0)
            
        }
        
        statusView.snp_makeConstraints { (make) -> Void in
            
            //记录这个约束
            constraintTop = make.top.equalTo(otherView.snp_bottom).constraint
            make.leading.trailing.equalTo(0)
            
            
        }
        
   
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(statusView)
            make.leading.trailing.top.equalTo(0)
        }
        
    }
    
    
    
    // MARK: -  懒加载子控件
    lazy var ownView:HZOwnCellView = {
       
        let ownView = HZOwnCellView()
        
        
        return ownView
    }()
    
    
    lazy var statusView:HZStatusView = {
        
       let statusView = HZStatusView()
        
        return statusView
    }()
    
    
    lazy var otherView:HZOtherView = {
        
       let otherView = HZOtherView()
        
        return otherView
    }()
    
    

}
